import json
import re
import copy
import os
import argparse


'''
tyoe: launch, task
language: python, c
'''

json_dump_params = dict(ensure_ascii=False, indent=4)

launch_templates = {
    "python": {
        "module": {
            "name": "Python: 模块",
            "type": "python",
            "request": "launch",
            "module": "mname",
            "justMyCode": True
        },
        "curr_file": {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}",
            "cwd": "${workspaceFolder}",
            "console": "integratedTerminal",
            "justMyCode": True
        }
    },
    "c": {
        "gdb_launch": {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "输入程序名称，例如 ${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": False,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": False,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": True
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": True
                }
            ]
        }
    }
}

tasks_templates = {
    "other": {
            "label": "echo",
            "type": "shell",
            "command": "echo Hello",
            "options": {
                "cwd": "${workspaceFolder}"
            }
        }
}

base_launch = {
    "version": "0.2.0",
    "configurations": [
        
    ]
}

base_task = {
    "version": "2.0.0",
    "tasks": [
       
    ]
}
def remove_comments(json_str):
    pattern = r'(?<!http:)//.*|/\*(.|\n)*?\*/'
    return re.sub(pattern, '', json_str)

def add_launch(language, ltype, savepath):
    data = None
    if os.path.exists(savepath):
        with open(savepath) as f:
            # data = json.load(f)
            strdata = f.read()
            strdata = remove_comments(strdata)
            data = json.loads(strdata)
            if 'configurations' in data:
                configurations = data['configurations']
            else:
                configurations = []
    else:
        data = copy.deepcopy(base_launch)
        configurations = data['configurations']

    configurations.append(launch_templates[language][ltype])
    data['configurations'] = configurations
    with open(savepath, 'w') as f:
        json.dump(data, f, **json_dump_params)

def add_task(savepath):
    data = None
    if os.path.exists(savepath):
        with open(savepath) as f:
            # data = json.load(f)
            strdata = f.read()
            strdata = remove_comments(strdata)
            data = json.loads(strdata)
            if 'tasks' in data:
                tasks = data['tasks']
            else:
                tasks = []
    else:
        data = copy.deepcopy(base_task)
        tasks = data['tasks']
    tasks.append(tasks_templates['other'])
    data['tasks'] = tasks
    with open(savepath, 'w') as f:
        json.dump(data, f, **json_dump_params)

if __name__ == '__main__':
    parse = argparse.ArgumentParser()
    parse.add_argument('--type')
    parse.add_argument('--ltype')
    parse.add_argument('--language')
    parse.add_argument('--savepath')
    args = parse.parse_args()
    if args.type == 'launch':
        add_launch(args.language, args.ltype, args.savepath)
    elif args.type == 'task':
        add_task(args.savepath)
    
